package com.sjtu.vfact.textual.view;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.SpinnerNumberModel;

import java.awt.Dimension;
import java.awt.Insets;

import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import java.awt.Color;
import javax.swing.border.BevelBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;

import javax.swing.JProgressBar;
import javax.swing.JSpinner;
import javax.swing.border.SoftBevelBorder;
import javax.swing.event.ChangeListener;
import javax.swing.event.TableModelListener;

import com.sjtu.vfact.textual.tablemodel.VFactTableModel;
import javax.swing.JTextArea;
import javax.swing.JComboBox;

public class FACTAFrame_V1 extends JFrame{

	private static final long serialVersionUID = -2430376380996509008L;

	private JFrame frmFactMiner;
	
	private JLabel lblStatusUpdate;
	
	private JTable tableMasterTermset;
	private JTable tableSummarizer;
	private JTable tableEnumerator;
	private JTable tableUncoveredTerms;
	
	private JTextField txtFldFileName;
	private JTextField txtFldLengthMin;
	private JTextField txtFldLengthMax;
	private JTextField txtFldNumberN;
	private JTextField txtFldP;
	private JTextField txtFldAfterMining;
	private JTextArea txtAreaCollecting;
	
	private JButton btnFactaA;
	private JButton btnFactaB;
	private JButton btnFactaC;
	private JButton btnFactaD;
	private JButton btnOpenFile;
	private JButton btnSum;
	private JButton btnEnum;
	private JButton btnShowGraph;
	private JButton btnSSA;
	private JButton btnShowSSAGraph;
	
	private JProgressBar prgBarCollecting;
	private JProgressBar prgBarCoverage;
	private JProgressBar prgBarSpecificity;
	private JProgressBar prgBarDensity;
	
	private JSpinner spinnerCSD;
	private JComboBox cmbBoxSSARanges;
	
	/**
	 * Create the application.
	 */
	public FACTAFrame_V1() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmFactMiner = new JFrame();
		frmFactMiner.setTitle("FACT Miner");
		frmFactMiner.setResizable(false);
		frmFactMiner.setBounds(100, 100, 1030, 773);
		frmFactMiner.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmFactMiner.getContentPane().setLayout(null);
		
		JPanel panelMasterTermsetTable = new JPanel();
		panelMasterTermsetTable.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panelMasterTermsetTable.setBounds(6, 6, 573, 287);
		frmFactMiner.getContentPane().add(panelMasterTermsetTable);
		
		JScrollPane scrollPaneMasterTermset = new JScrollPane();
		scrollPaneMasterTermset.setPreferredSize(new Dimension(555, 275));
		panelMasterTermsetTable.add(scrollPaneMasterTermset);
		
		tableMasterTermset = new JTable();
		scrollPaneMasterTermset.setViewportView(tableMasterTermset);
		
		JPanel panelSummarizerTable = new JPanel();
		panelSummarizerTable.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panelSummarizerTable.setBounds(6, 305, 573, 147);
		frmFactMiner.getContentPane().add(panelSummarizerTable);
		
		JScrollPane scrollPaneSummarizer = new JScrollPane();
		scrollPaneSummarizer.setPreferredSize(new Dimension(555, 135));
		panelSummarizerTable.add(scrollPaneSummarizer);
		
		tableSummarizer = new JTable();
		scrollPaneSummarizer.setViewportView(tableSummarizer);
		
		JPanel panelEnumeratorTable = new JPanel();
		panelEnumeratorTable.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panelEnumeratorTable.setBounds(6, 464, 573, 221);
		frmFactMiner.getContentPane().add(panelEnumeratorTable);
		
		JScrollPane scrollPaneEnumerator = new JScrollPane();
		scrollPaneEnumerator.setPreferredSize(new Dimension(555, 208));
		panelEnumeratorTable.add(scrollPaneEnumerator);
		
		tableEnumerator = new JTable();
		scrollPaneEnumerator.setViewportView(tableEnumerator);
		
		JPanel panelMasterCollectionGeneration = new JPanel();
		panelMasterCollectionGeneration.setBorder(new CompoundBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Master Collection Generation", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)), new BevelBorder(BevelBorder.LOWERED, null, null, null, null)));
		panelMasterCollectionGeneration.setBounds(599, 6, 415, 287);
		frmFactMiner.getContentPane().add(panelMasterCollectionGeneration);
		panelMasterCollectionGeneration.setLayout(null);
		
		JLabel lblFileName = new JLabel("File Name : ");
		lblFileName.setBounds(16, 41, 98, 16);
		panelMasterCollectionGeneration.add(lblFileName);
		
		JLabel lblStatus = new JLabel("Status :");
		lblStatus.setBounds(16, 75, 98, 16);
		panelMasterCollectionGeneration.add(lblStatus);
		
		JLabel lblLength = new JLabel("Length (L) min - max :");
		lblLength.setBounds(16, 103, 162, 16);
		panelMasterCollectionGeneration.add(lblLength);
		
		JLabel lblNumber = new JLabel("Number (N) :");
		lblNumber.setBounds(16, 135, 98, 16);
		panelMasterCollectionGeneration.add(lblNumber);
		
		txtFldFileName = new JTextField();
		txtFldFileName.setBounds(117, 35, 235, 28);
		txtFldFileName.setEditable(false);
		panelMasterCollectionGeneration.add(txtFldFileName);
		txtFldFileName.setColumns(100);
		
		lblStatusUpdate = new JLabel();
		lblStatusUpdate.setBounds(117, 75, 235, 16);
		panelMasterCollectionGeneration.add(lblStatusUpdate);
		
		txtFldLengthMin = new JTextField();
		txtFldLengthMin.setBounds(171, 97, 76, 28);
		txtFldLengthMin.setColumns(10);
		txtFldLengthMin.setText("1");
		panelMasterCollectionGeneration.add(txtFldLengthMin);
		
		txtFldLengthMax = new JTextField();
		txtFldLengthMax.setColumns(10);
		txtFldLengthMax.setBounds(288, 97, 76, 28);
		txtFldLengthMax.setText("100");
		panelMasterCollectionGeneration.add(txtFldLengthMax);
		
		JLabel lblMinMax = new JLabel("-");
		lblMinMax.setBounds(262, 103, 28, 16);
		panelMasterCollectionGeneration.add(lblMinMax);
		
		txtFldNumberN = new JTextField();
		txtFldNumberN.setColumns(10);
		txtFldNumberN.setBounds(117, 129, 76, 28);
		txtFldNumberN.setText("100");
		panelMasterCollectionGeneration.add(txtFldNumberN);
		
		JLabel lblP = new JLabel("P : ");
		lblP.setBounds(216, 135, 42, 16);
		panelMasterCollectionGeneration.add(lblP);
		
		txtFldP = new JTextField();
		txtFldP.setColumns(10);
		txtFldP.setBounds(262, 129, 76, 28);
		txtFldP.setText("5");
		panelMasterCollectionGeneration.add(txtFldP);
		
		JPanel panelButtonFacta = new JPanel();
		panelButtonFacta.setBorder(new TitledBorder(null, "FACTA", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelButtonFacta.setBounds(16, 163, 202, 100);
		panelMasterCollectionGeneration.add(panelButtonFacta);
		panelButtonFacta.setLayout(null);
		
		btnFactaA = new JButton("FACTA-A");
		btnFactaA.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
		btnFactaA.setBounds(10, 22, 84, 29);
		panelButtonFacta.add(btnFactaA);
		
		btnFactaB = new JButton("FACTA-B");
		btnFactaB.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
		btnFactaB.setBounds(10, 56, 84, 29);
		panelButtonFacta.add(btnFactaB);
		
		btnFactaC = new JButton("FACTA-C");
		btnFactaC.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
		btnFactaC.setBounds(104, 22, 82, 29);
		panelButtonFacta.add(btnFactaC);
		
		btnFactaD = new JButton("FACTA-D");
		btnFactaD.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
		btnFactaD.setBounds(104, 56, 82, 29);
		panelButtonFacta.add(btnFactaD);
		
		prgBarCollecting = new JProgressBar(0, 100);
		prgBarCollecting.setValue(0);
		prgBarCollecting.setStringPainted(true);
		prgBarCollecting.setBounds(226, 169, 166, 20);
		panelMasterCollectionGeneration.add(prgBarCollecting);
		
		btnOpenFile = new JButton("...");
		btnOpenFile.setBounds(364, 40, 28, 21);
		panelMasterCollectionGeneration.add(btnOpenFile);
		
		JScrollPane scrollPaneTxtAreaCollecting = new JScrollPane();
		scrollPaneTxtAreaCollecting.setBounds(230, 201, 162, 62);
		panelMasterCollectionGeneration.add(scrollPaneTxtAreaCollecting);
		
		txtAreaCollecting = new JTextArea();
		txtAreaCollecting.setMargin(new Insets(5, 5, 5, 5));
		txtAreaCollecting.setEditable(false);
		scrollPaneTxtAreaCollecting.setViewportView(txtAreaCollecting);
		
		JPanel panelQualityInspector = new JPanel();
		panelQualityInspector.setBorder(new CompoundBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Quality Inspector", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)), new BevelBorder(BevelBorder.LOWERED, null, null, null, null)));
		panelQualityInspector.setBounds(599, 293, 415, 120);
		frmFactMiner.getContentPane().add(panelQualityInspector);
		panelQualityInspector.setLayout(null);
		
		JLabel lblCsdInSupport = new JLabel("CSD in Support : ");
		lblCsdInSupport.setBounds(22, 76, 108, 16);
		panelQualityInspector.add(lblCsdInSupport);
		
		spinnerCSD = new JSpinner(new SpinnerNumberModel(2, 2, 20, 1));
		spinnerCSD.setBounds(132, 67, 47, 35);
		spinnerCSD.setEnabled(false);
		panelQualityInspector.add(spinnerCSD);
		
		prgBarCoverage = new JProgressBar(0, 100);
		prgBarCoverage.setBounds(189, 33, 120, 20);
		panelQualityInspector.add(prgBarCoverage);
		
		prgBarSpecificity = new JProgressBar(0, 100);
		prgBarSpecificity.setBounds(189, 58, 120, 20);
		panelQualityInspector.add(prgBarSpecificity);
		
		prgBarDensity = new JProgressBar(0, 100);
		prgBarDensity.setBounds(189, 82, 120, 20);
		panelQualityInspector.add(prgBarDensity);
		
		JLabel lblCoverage = new JLabel("Coverage");
		lblCoverage.setBounds(321, 37, 76, 16);
		panelQualityInspector.add(lblCoverage);
		
		JLabel lblSpecificity = new JLabel("Specificity");
		lblSpecificity.setBounds(321, 58, 76, 16);
		panelQualityInspector.add(lblSpecificity);
		
		JLabel lblDensity = new JLabel("Density");
		lblDensity.setBounds(321, 82, 76, 16);
		panelQualityInspector.add(lblDensity);
		
		JPanel panelAfterMiningTask = new JPanel();
		panelAfterMiningTask.setBorder(new CompoundBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "After-Mining Task", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)), new BevelBorder(BevelBorder.LOWERED, null, null, null, null)));
		panelAfterMiningTask.setBounds(599, 416, 415, 71);
		frmFactMiner.getContentPane().add(panelAfterMiningTask);
		
		btnEnum = new JButton("Enum");
		btnEnum.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
		panelAfterMiningTask.add(btnEnum);
		
		btnSum = new JButton("Sum");
		btnSum.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
		panelAfterMiningTask.add(btnSum);
		
		txtFldAfterMining = new JTextField();
		panelAfterMiningTask.add(txtFldAfterMining);
		txtFldAfterMining.setColumns(10);
		
		btnSSA = new JButton("SSA");
		panelAfterMiningTask.add(btnSSA);
		
		JPanel panelUncoveredTerms = new JPanel();
		panelUncoveredTerms.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panelUncoveredTerms.setBounds(599, 488, 415, 197);
		frmFactMiner.getContentPane().add(panelUncoveredTerms);
		
		JScrollPane scrollPaneUncoveredTerms = new JScrollPane();
		scrollPaneUncoveredTerms.setPreferredSize(new Dimension(400, 185));
		panelUncoveredTerms.add(scrollPaneUncoveredTerms);
		
		tableUncoveredTerms = new JTable();
		scrollPaneUncoveredTerms.setViewportView(tableUncoveredTerms);
		
		JPanel panel = new JPanel();
		panel.setBounds(6, 706, 1008, 39);
		frmFactMiner.getContentPane().add(panel);
		
		btnShowGraph = new JButton("Show Graph");
		panel.add(btnShowGraph);
		
		cmbBoxSSARanges = new JComboBox();
		cmbBoxSSARanges.setPreferredSize(new Dimension(130, 27));
		panel.add(cmbBoxSSARanges);
		
		btnShowSSAGraph = new JButton("Show SSA Graph");
		panel.add(btnShowSSAGraph);
	}
	
	public JFrame getFrame(){
		return this.frmFactMiner;
	}
	
	public void displayFrame(){
		frmFactMiner.setVisible(true);
	}
	
	public void addBtnFactaAListener(ActionListener l){
		btnFactaA.addActionListener(l);
	}
	
	public void addBtnFactaBListener(ActionListener l){
		btnFactaB.addActionListener(l);
	}
	
	public void addBtnFactaCListener(ActionListener l){
		btnFactaC.addActionListener(l);
	}
	
	public void addBtnFactaDListener(ActionListener l){
		btnFactaD.addActionListener(l);
	}
	
	public void addBtnOpenFileListener(ActionListener l){
		btnOpenFile.addActionListener(l);
	}
	
	public void addBtnSumListener(ActionListener l){
		btnSum.addActionListener(l);
	}
	
	public void addBtnEnumListener(ActionListener l){
		btnEnum.addActionListener(l);
	}
	
	public void addBtnShowGraphListener(ActionListener l){
		btnShowGraph.addActionListener(l);
	}
	
	public void addBtnSSAListener(ActionListener l){
		btnSSA.addActionListener(l);
	}
	
	public void addBtnShowSSAGraphListener(ActionListener l){
		btnShowSSAGraph.addActionListener(l);
	}
	
	public void addSpinnerCSDListener(ChangeListener l){
		spinnerCSD.addChangeListener(l);
	}
	
	public void addTableMasterTermsetListener(TableModelListener l){
		tableMasterTermset.getModel().addTableModelListener(l);
	}
	
	public void addTableSummarizerListener(TableModelListener l){
		tableSummarizer.getModel().addTableModelListener(l);
	}
	
	public void addTableEnumeratorListerner(TableModelListener l){
		tableEnumerator.getModel().addTableModelListener(l);
	}
	
	public void addTableUncoveredTermsListener(TableModelListener l){
		tableUncoveredTerms.getModel().addTableModelListener(l);
	}
	
	public void setTableMasterTermsetModel(VFactTableModel tableModel){
		tableMasterTermset.setModel(tableModel);
	}
	
	public void setTableSummarizerModel(VFactTableModel tableModel){
		tableSummarizer.setModel(tableModel);
	}
	
	public void setTableEnumeratorModel(VFactTableModel tableModel){
		tableEnumerator.setModel(tableModel);
	}
	
	public void setTableUncoveredTermsModel(VFactTableModel tableModel){
		tableUncoveredTerms.setModel(tableModel);
	}

	public void setSpinnerCSDValue(int minValue, int maxValue){
		SpinnerNumberModel model = (SpinnerNumberModel) spinnerCSD.getModel();
		model.setMinimum(minValue);
		model.setMaximum(maxValue);
	}
	
	public void setEnabledSpinnerCSD(boolean state){
		spinnerCSD.setEnabled(state);
	}
	
	public String getTxtFldFileName(){
		return txtFldFileName.getText();
	}
	
	public void setTxtFldFileName(String text){
		txtFldFileName.setText(text);
	}
	
	public void setLblStatus(String text){
		lblStatusUpdate.setText(text);
	}
	
	public void appendTxtAreaCollecting(String text){
		txtAreaCollecting.append(text);
	}
	
	public void clearTxtAreaCollecting(){
		txtAreaCollecting.setText("");
	}
	
	public int getLengthMin(){
		return Integer.parseInt(txtFldLengthMin.getText());
	}
	
	public int getLengthMax(){
		return Integer.parseInt(txtFldLengthMax.getText());
	}
	
	public int getNumberN(){
		return Integer.parseInt(txtFldNumberN.getText());
	}
	
	public int getP(){
		return Integer.parseInt(txtFldP.getText());
	}
	
	public String getSSARanges(){
		return txtFldAfterMining.getText();
	}
	
	public void setCollectingProgress(int progress){
		prgBarCollecting.setValue(progress);
	}
	
	public void setCoverageValue(int coverage){
		prgBarCoverage.setValue(coverage);
	}
	
	public void setSpecificityValue(int specificity){
		prgBarSpecificity.setValue(specificity);
	}
	
	public void setDensityValue(int density){
		prgBarDensity.setValue(density);
	}
	
	public Object getSelectedSSARange(){
		return cmbBoxSSARanges.getSelectedItem();
	}
	
	public void setSelectedSSARangeIndex(int index){
		cmbBoxSSARanges.setSelectedIndex(index);
	}
	
	public void setSSARangeModel(DefaultComboBoxModel model){
		cmbBoxSSARanges.setModel(model);
	}
	
	public int getSelectedSSARangeIndex(){
		return cmbBoxSSARanges.getSelectedIndex();
	}
	
	public int[] getMasterTermsetSelectedRows(){
		return tableMasterTermset.getSelectedRows();
	}
}
